package com.xm.settlement.controller;

import com.xm.settlement.commons.base.BaseController;
import com.xm.settlement.commons.utils.PageInfo;
import com.xm.settlement.model.MessageManage;
import com.xm.settlement.service.IMessageManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/30.
 */
@RequestMapping("/messageManage")
@Controller
public class MessageManageController extends BaseController{

    @Autowired
    private IMessageManageService messageCenterService;

    /**
     * 留言板主页
     *
     * @return
     */
    @GetMapping(value = "/manager")
    public String Manager() {
        return "serviceCenter/messageManage";
    }

    /**
     * 留言板管理
     * @param page
     * @param rows
     * @param order
     * @return
     */
    @PostMapping("/dataGrid")
        @ResponseBody
        public Object dataGrid(Integer page, Integer rows, String sort, String order) {
            PageInfo pageInfo = new PageInfo (page, rows, sort, order);
            Long createId = getUserId();
            Map<String, Object> condition = new HashMap<String, Object>();
            condition.put("createId",createId);
            pageInfo.setCondition(condition);
            messageCenterService.selectDataGrid(pageInfo);
            return pageInfo;
    }

    /**
     * 添加留言板页
     *
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage() {
        return "serviceCenter/messageManageAdd";
    }


    /**
     * 添加留言板
     *
     * @param messageManage
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Object add(MessageManage messageManage) {
        messageManage.setCreateId(getUserId());
        messageManage.setCreateTime(new Date());
        messageCenterService.insert(messageManage);
        return renderSuccess("添加成功！");
    }


    /**
     * 编辑留言板页
     *
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/editPage")
    public String editPage(Model model, Long id) {
        MessageManage messageManage = messageCenterService.selectById(id);
        model.addAttribute("messageManage", messageManage);
        return "serviceCenter/messageManageEdit";
    }


    /**
     * 编辑留言板
     *
     * @param messageManage
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(MessageManage messageManage) {
        messageManage.setUpdateId(getUserId());
        messageManage.setUpdateTime(new Date());
        messageCenterService.updateSelectiveById(messageManage);
        return renderSuccess("编辑成功！");
    }


    /**
     * 删除留言板
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(Long id) {
        messageCenterService.deleteById(id);
        return renderSuccess("删除成功！");
    }
}